package com.cityos.android.mystudynestedscroll.ui.activity

import android.content.Context
import android.graphics.Color
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.AttributeSet
import android.view.View
import androidx.appcompat.widget.AppCompatImageView
import androidx.appcompat.widget.AppCompatTextView
import androidx.coordinatorlayout.widget.CoordinatorLayout
import com.cityos.android.mystudynestedscroll.ui.view.CustomView
import com.cityos.android.mystudynestedscroll.R
import com.cityos.android.mystudynestedscroll.databinding.ActivityEasyBehaviorBinding

class EasyBehaviorActivity : AppCompatActivity() {

    private lateinit var mainBinding: ActivityEasyBehaviorBinding
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mainBinding = ActivityEasyBehaviorBinding.inflate(layoutInflater).apply {
            setContentView(this.root)
        }
        mainBinding.iv.setBackgroundColor(Color.BLACK)

    }
}

class ImageViewBehavior(context: Context? , attrs: AttributeSet?) : CoordinatorLayout.Behavior<AppCompatTextView>(context , attrs) {

    override fun layoutDependsOn(parent: CoordinatorLayout , child: AppCompatTextView , dependency: View): Boolean {
        return dependency is CustomView
    }


    override fun onDependentViewChanged(parent: CoordinatorLayout , child: AppCompatTextView , dependency: View): Boolean {
        val imageView = dependency as? AppCompatImageView ?: return false
        child.translationY = imageView.top.toFloat()
        child.translationX = imageView.left.toFloat()
        return true
    }

    override fun onLayoutChild(parent: CoordinatorLayout , child: AppCompatTextView , layoutDirection: Int): Boolean {
        val imageView = parent.findViewById<AppCompatImageView>(R.id.iv)
        child.layout(0 , imageView.height , parent.width , parent.height)
        return true
    }

}
